package jmars.mobile.worker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;

public class Export implements Investigator {
	Map<Integer,String[]> condition=new HashMap<Integer,String[]>();

	public Export(){
		init();
	}
	
	private void init(){
		String[] sgsns={"220.206.188.12",
		                "220.206.188.10",
		                "220.206.188.11",
		                "220.206.150.156",
		                "220.206.150.155",
		                "220.206.150.154",
		                "220.206.150.124",
		                "220.206.150.132",
		                "220.206.150.115",
		                "220.206.188.49",
		                "220.206.188.60",
		                "220.206.188.51",
		                "220.206.188.50",
		                "220.206.188.53",
		                "220.206.188.52",
		                "220.206.188.55",
		                "220.206.188.54",
		                "220.206.188.57",
		                "220.206.188.56",
		                "220.206.188.58",
		                "220.206.188.59",
		                "220.206.188.2",
		                "220.206.188.1",
		                "220.206.188.4",
		                "220.206.188.3",
		                "220.206.188.9",
		                "220.206.188.6",
		                "220.206.188.5",
		                "220.206.188.8",
		                "220.206.188.7"};
		condition.put(18, sgsns);
	}
	
	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException {
		Long total = 0L;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0,000");
		boolean flag=false;
//		String sf="",gf="";
//		int sfc=0,gfc=0;
		for(File inputFile:files){
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			while((row=reader.readLine())!=null){
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRow(cols,false,true)) continue;
				flag=false;
//				if(cols[18].equals("220.206.188.104")){
//					sf=cols[0];sfc++;
//				}
//				if(cols[19].equals("220.206.188.104")){
//					gf=cols[0];gfc++;
//				}
				for(Integer cidx:condition.keySet()){
					for(String s:condition.get(cidx)){
						if(cols[cidx].equals(s)){
							flag=true;
							break;
						}
					}
					if(flag)break;
				}
				if(!flag)continue;
				total++;
				if(Summary.targetColumns==null||"".equals(Summary.targetColumns.trim())){
					writer.write(row+"\r\n");
					continue;
				}
				for(Integer idx:Summary.targetColumnIdx){
					writer.write(cols[idx]);
					if(idx!=Summary.targetColumnIdx[Summary.targetColumnIdx.length-1]){
						writer.write("|");
					}
				}
				writer.write("\r\n");
			}
			reader.close();
			fis.close();
			System.out.println(inputFile.getName()+" finished at "+total);
		}
//		System.out.println("sf: "+sf+","+sfc);
//		System.out.println("gf: "+gf+","+gfc);
		return total;
	}
}